<div class="pageHeader">
    <div class="searchBar">
        <table class="searchContent">
            <tr>
                <td style="width:110px ">
                    <select class="combox" id="mode" name="mode" ref="source" refUrl="<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=ajaxGetSource&mode={value}">
                        <option value="">请选择抓取方式</option>
                        <option value="1">单条抓取</option>
                        <option value="2">批量抓取</option>
                    </select>
                </td>
                <td style="width:130px ">
                    <select class="combox" name="source" id="source">
                        <option value="">请选择抓取来源</option>
                    </select>
                </td>
                <td>
                    抓取信息地址：<input type="text" name="source_url"  size="80" value="http://www.36kr.com/p/205293.html"/>
                </td>
            </tr>
            <tr id="new_nums" style="display: none">
                <td style="color: blue" id="all_num">
                   总共：<span>0</span>条
                </td>
                <td style="color: blue" id="success_num">
                    成功：<span>0</span>条
                </td>
                <td style="color: red" id="failed_num">
                    失败：<span>0</span>条
                </td>
            </tr>
        </table>
        <div class="subBar">
            <ul>
                <li><a class="button _crawl" href="###" title="点击抓取"><span>点击抓取</span></a></li>
            </ul>
        </div>
    </div>
</div>
<div class="pageContent">
    <div class="panelBar">
        <ul class="toolBar">
            <li><a class="add _add_news" href="###"><span>抓取信息入库</span></a></li>
            <li class="line">line</li>
            <li><a class="edit _edit_news" href="###"><span>从失败处抓取入库</span></a></li>
            <li class="line">line</li>
            <li><a class="delete _reset_news" href="###"><span>抓取信息清空</span></a></li>
        </ul>
    </div>
    <div class="pageFormContent _news_sigle" layoutH="97" style="display: none">
        <p style="width:800px">
            <label style="width: 80px">资讯名称：</label>
            <input name="title" id="title" type="text" size="70" value=""/>
            <input name="source_id" id="source_id" type="hidden" value=""/>
            <input name="time" id="time" type="hidden" value=""/>
        </p>
        <p style="width:800px">
            <label style="width: 80px">资讯标签：</label>
            <input name="tag" id="tag" type="text" size="70" value="" />
        </p>
        <p style="width:800px">
            <label style="width: 80px">抓取类型：</label>
            <label id="curr_category">其它</label>
        </p>
        <p style="width:800px">
            <label style="width: 80px">资讯类型：</label>
            <select class="combox" name="category_id" id="category_id">
                <?php
                    foreach((array)$category_list as $k=>$v){
                ?>
                <option value="<?php echo $k; ?>" <?php echo 0 == $k ? 'selected="true"' : ''; ?>><?php echo $v; ?></option>
                <?php } ?>
            </select>
        </p>
        <p style="width:800px">
            <label style="width: 80px;font-weight: bold;color: red">资讯状态：</label>
            <select class="combox" name="status" id="status">
                <?php
                foreach((array)$status_list as $k=>$v){
                    ?>
                    <option value="<?php echo $k; ?>" <?php echo 0 == $k ? 'selected="true"' : ''; ?>><?php echo $v; ?></option>
                <?php } ?>
            </select>
        </p>
        <p style="width:800px;height: 210px">
            <label style="width: 80px">资讯缩图：</label>
            <select id="s_picture" name="s_picture" class="combox">
            <?php for($i=1;$i<=15;$i++){ //预设值供选择?>
                <option value="<?php echo $i; ?>" <?php echo $i == 1 ? 'selected="true"' : ''; ?> >内容第<?php echo $i; ?>张图片</option>
            <?php } ?>
            </select>
            <input name="picture" id="picture" type="hidden" value=""/>
            <img id="picture_show" src="../Upload/default/320x200.jpg" style="border: 1px solid #000000" width="320" height="200">
        </p>
        <p style="width:800px;height: 50px">
            <label style="width: 80px">关键词组：</label>
            <textarea rows="2" cols="80" name="keywords" id="keywords" class="textInput"></textarea>
        </p>
        <p style="width:800px;height: 50px">
            <label style="width: 80px">资讯描述：</label>
            <textarea rows="2" cols="80" name="description" id="description" class="textInput"></textarea>
        </p>
        <p style="width:800px;height: 50px">
            <label style="width: 80px">资讯摘要：</label>
            <textarea rows="2" cols="80" name="summary" id="summary" class="textInput"></textarea>
        </p>
        <p style="width:900px;height: 330px">
            <label style="width: 80px">资讯内容：</label>
            <textarea name="content" id="content" style="height: 300px; width: 800px;"></textarea>
        </p>
    </div>
    <table class="table" width="100%" layoutH="162" >
        <thead class="_news_batch" style="display: none">
        <tr>
            <th width="40" class="center">抓取编码</th>
            <th width="300" class="center">资讯名称</th>
            <th width="80" class="center">资讯类型</th>
            <th width="40" class="center">操作</th>
        </tr>
        </thead>
        <tbody class="_news_batch" style="display: none">
        </tbody>
    </table>
</div>
<script type="text/javascript">

    $(document).ready(function(){

		var top_num = 0;
        //动态加载 Kindeditor 文本编辑器
        var  content ;
        $.getScript('<?php echo SITE_URL; ?>/theme/js/kindeditor-4.1.7/kindeditor-min.js', function() {
            KindEditor.basePath = '<?php echo SITE_URL."/theme/js/kindeditor-4.1.7/"; ?>';
            content = KindEditor.create('textarea[name="content"]', {
                themeType: 'simple',
                resizeType: 1,
                uploadJson: '<?php echo SITE_URL . 'index.php?c=new&a=newPictureUpload&type=new';?>',
                fileManagerJson: '<?php echo SITE_URL . 'index.php?c=new&a=newDirPicture';?>',
                allowFileManager: true,
                //经测试，下面这行代码可有可无，不影响获取textarea的值
                //afterCreate: function(){this.sync();}
                //下面这行代码就是关键的所在，当失去焦点时执行 this.sync();
                afterBlur: function () { this.sync(); }
            });
        });

        //抓取信息操作
        var mode_flag = '';//当前抓取模式
        var list_num = 0;//批量抓取数据完成时的总条数

        //刷新页面 取消相关绑定事件
        $('._crawl,._reset_news,._add_news,._edit_news,._remove_new').unbind('click');
        $('select[name=s_picture]').unbind('change')

        //重置操作
        $('._reset_news').bind('click',function(){
            $('._news_sigle').hide();
            $('._news_batch').hide();
            $('input').val('');
            $('textarea').html('');
            $('tbody._news_batch').html('');
			top_num = 0;
            $('#new_nums').find('span').text(0);
        });

        //移除当前选中行的新闻
        $('._remove_new').live('click',function(){
            $(this).parents('tr').remove();
        });

        $('._ref_new').live('click',function(){
            if($(this).hasClass('_ref_new_end')){
                alertMsg.info('数据已入库成功，请不要重复重试！');
            }else{
                pollAddInfo($(this).parents('tr'));
            }
        });

        //单条数据抓取 选择 头图操作
        $('#s_picture').bind('change',function(){
            var select_link = $(this).find("option:selected").attr('link');
            $('#picture_show').attr('src',select_link);
            $('input[name=picture]').val(select_link);
        });
        //重新选择类型清空数据
        $('#mode').bind('change',function(){
            $('input[name=source_url]').val('');
            $('._news_sigle').hide();
            $('._news_batch').hide();
        });

        //抓取操作
        $('._crawl').bind('click',function(){
            var curr_mode = $('select[name=mode]').val();
            var curr_source = $('select[name=source]').val();
            var curr_source_url = $('input[name=source_url]').val();

            if(!curr_mode){
                alertMsg.info('请选择抓取方式');
                return false;
            }

            if(!curr_source){
                alertMsg.info('请选择抓取来源');
                return false;
            }

            if(!curr_source_url){
                alertMsg.info('请填写抓取信息的地址');
                return false;
            }

            if(curr_mode == 1){//单条
                var url_tmp= new Array();//临时变量，保存分割字符串
                url_tmp=curr_source_url.split("/");//按照"/"分割
                var p_tmp = url_tmp[url_tmp.length-1];//获取最后一部分，即文件名和参数
                var p_id =p_tmp.split(".");//把参数和文件名分割开

                $.getJSON("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=getSigle&source="+curr_source+"&p_id="+p_id[0], {}, function(json){
                    if(json.retCode == 200){
                        //循环表单赋值
                        $.each(json, function(i,val){
                            if(i == 'content'){
                                content.html(val);
                                content.sync();
                            }else if(i == 's_picture'){
                                var str = '';
                                var curr_i = 0;
                                var show_img = false;
                                $.each(val, function(p_i,p_val){
                                    if(show_img == false){

                                        $('#picture_show').attr('src',p_val);
                                        $('input[name=picture]').val(p_val);
                                        show_img = true;
                                    }
                                    str += '<option value="'+(parseInt(p_i)+1)+'" link="'+p_val+'">内容第'+(parseInt(p_i)+1)+'图片</option>';
                                    curr_i = parseInt(p_i);
                                });

                                $('#op_combox_s_picture li').each(function(i){
                                    if(i>curr_i){
                                        $(this).remove();
                                    }
                                });

                                $("#"+i).html(str);
                            }else if(i == 'curr_category' && val != null){
                                $("#"+i).text(val);
                            }else{
                                $("#"+i).val(val);
                            }
                        });

                        $('._news_sigle').show();
                        $('._news_batch').hide();
                        mode_flag = 'sigle';

                    }else{
                        alertMsg.error('单个资讯抓取失败,请刷新后重试!');
                    }
                });
            }else{

                $('tbody._news_batch').html('');
                $('#new_nums').find('span').text(0);

                list_num = 0;
                var url_tmp= parseURL(curr_source_url);

                if(url_tmp){
                    pollSpider(url_tmp['file'],parseInt(url_tmp['params']['page']));
                }

                $('._news_sigle').hide();
                $('._news_batch').show();
                mode_flag = 'batch';
            }

        });

        //轮询显示准备抓取入库的数据
        function pollSpider(type,page){
            $.getJSON("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=getBatch&type="+type+"&page="+page, {}, function(json){
                if(json.retCode == 200){
                    var curr_class = 'trbg';//class="trbg"
                    $.each(json, function(i,val){
                        if(i != 'retCode'){
                            var str = '';
                            curr_class = (curr_class == 'trbg' ? '' : 'trbg');

                            str = '<tr class="'+curr_class+ '" href_url = "'+val['href']+'" img_src = "'+val['src']+'" title="'+val['title']+'" type="'+val['type']+'" time ="'+val['time']+'" summary="'+val['summary']+'">'+
                                '<td width="40" class="center">P'+page+'->'+(parseInt(i)+1)+'：</td>'+
                                '<td width="300" class="center">'+val['title']+'</td>'+
                                '<td width="80" class="center">'+val['type']+'</td>'+
                                '<td width="40" class="center"><a style="cursor:pointer;color: #0000ff" class="_ref_new">重试</a>&nbsp;&nbsp;<a style="cursor:pointer;color: #0000ff" class="_remove_new">移除</a></td>'+
                                '</tr>';

                            $('tbody._news_batch').append(str);

                            list_num++;
                        }
                    });

                    if(parseInt(page - 1) > 0){
                        pollSpider(type,(parseInt(page) - 1));
                    }else{
                        $('#new_nums').show();
                        $('#all_num').find('span').text(list_num);
                    }
                }
            });
        }

        $('._add_news').bind('click',function(){

            if(mode_flag == ''){
                alertMsg.info('请先选择类型&来源，并且输入抓取地址，抓取信息后操作！')
                return false;
            }

            if(mode_flag == 'sigle'){
                sigleAddInfo();
            }else if(mode_flag == 'batch'){
                pollAddInfo($('tbody._news_batch tr').eq(0),true);
            }

        });

        $('._edit_news').bind('click',function(){

            if(mode_flag == ''){
                alertMsg.info('请先选择类型&来源，并且输入抓取地址，抓取信息后操作！')
                return false;
            }

            $('tbody._news_batch tr').each(function(i){
                if($(this).css('color') == 'rgb(0, 0, 255)'){
                    $(this).remove();
					top_num = 0;
                }
            });
        });

        //单条数据入库
        function sigleAddInfo(){
            var _sigle = $('div._news_sigle');

            var category_id = _sigle.find('select[name=category_id]').val();
            var source_id = _sigle.find('input[name=source_id]').val();
            var title = _sigle.find('input[name=title]').val();
            var keywords = _sigle.find('textarea[name=keywords]').val();
            var description = _sigle.find('textarea[name=description]').val();
            var status = _sigle.find('select[name=status]').val();
            var summary = _sigle.find('textarea[name=summary]').val();
            var crawl_time = _sigle.find('input[name=time]').val();;
            var content = _sigle.find('textarea[name=content]').val();
            var tag = _sigle.find('input[name=tag]').val();
            var picture = _sigle.find('input[name=picture]').val();

            $.post("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=ajaxSigleAddNew",{category_id:category_id,source_id:source_id,title:title,keywords:keywords,description:description,status:status,summary:summary,crawl_time:crawl_time,content:content,tag:tag,picture:picture},function(data){
                if(data.retCode == 200){
                    alertMsg.correct('单个抓取入库成功！')
                    return false;
                }else{
                    alertMsg.error('单个资讯抓取入库失败!');
                    return false;
                }
            },"json");
        }

        //批量轮询数据抓取
        function pollAddInfo(dom,is_next){
            var _self = dom;

            if(_self.attr('href_url') == undefined){
                _self.css("color","red");
                $(_self).find('td').last().text('href_url为空！');
                return false;
            }

            var url_tmp= new Array();//临时变量，保存分割字符串
            url_tmp=_self.attr('href_url').split("/");//按照"/"分割
            var p_tmp = url_tmp[url_tmp.length-1];//获取最后一部分，即文件名和参数
            var p_id =p_tmp.split(".");//把参数和文件名分割开

            $.getJSON("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=ajaxAddNew", { p_id:p_id[0],src:_self.attr('img_src'),title:_self.attr('title'),summary:_self.attr('summary'),category:_self.attr('type'),time:_self.attr('time')}, function(json){
                _self.css("color",json.status == 1 ? "blue" : "red");

                if(is_next == true){
                    $(".gridScroller").animate({scrollTop:top_num},1000);

                    //top_num +=_self.height();
                    top_num += 22;

                    if(_self.next().first()){
                        pollAddInfo(_self.next().first(),true);
                    }

                    if(json.status == 1){
                        _self.find('a._ref_new').addClass('_ref_new_end');
                        $('#success_num').find('span').text(parseInt($('#success_num').find('span').text())+1);
                    }else{
                        $('#failed_num').find('span').text(parseInt($('#failed_num').find('span').text())+1);
                    }
                }else{

                    if(json.status == 1){
                        _self.find('a._ref_new').addClass('_ref_new_end');
                        $('#success_num').find('span').text(parseInt($('#success_num').find('span').text())+1);
                        $('#failed_num').find('span').text(parseInt($('#failed_num').find('span').text())-1);
                    }
                }
            });
        }

        setTimeout(function(){
            $('._news_sigle').hide();
            $('._news_batch').hide();
            mode_flag = '';
        },500);

    });

</script>
<!--通用处理js-->
<script type="text/javascript" src="<?php echo SITE_URL . 'theme/js/v1/admin_common.js'; ?>"></script>